﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace CooStationsToolsNew
{
	#region  工程的配置操作类

	/// <summary>
	/// 配置的操作类。
	/// </summary>
	public static class XMLInfoOP<T> where T : new()
	{
		static private object lockobj = new object();
		/// <summary>
		/// 获取XML配置信息(可提供路径)
		/// </summary>
		/// <param name="url"></param>
		/// <returns></returns>
		public static T GetInfo(string url)
		{
			T data = default(T);
			XmlSerializer serializer = new XmlSerializer(typeof(T));
			try
			{
				string fileName = url;
				FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
				data = (T)serializer.Deserialize(fs);
				fs.Close();
			}
			catch (Exception e)
			{
				data = new T();
			}
			return data;
		}

		/// <summary>
		/// 保存XML配置 (可提供路径)
		/// </summary>
		/// <param name="data"></param>
		/// <param name="url"></param>
		public static void SaveInfo(T data, string url)
		{
			FileOP.CreatFolder(url);
			string fileName = url;
			XmlSerializer serializer = new XmlSerializer(typeof(T));

			// serialize the object
			lock (lockobj)
			{
				FileStream fs = new FileStream(fileName, FileMode.Create);
				serializer.Serialize(fs, data);
				fs.Close();
			}
		}

		/// <summary>
		/// 将对象序列化成XML字符串
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		public static string GetXML(T data)
		{
			if (data == null) return "";
			using (StringWriter sw = new StringWriter())
			{
				XmlSerializer serializer = new XmlSerializer(typeof(T));
				serializer.Serialize(sw, data);
				return sw.ToString();
			}

		}
		/// <summary>
		/// 将XML字符串转换为指定对象
		/// </summary>
		/// <param name="xml"></param>
		/// <returns></returns>
		public static T GetObj(string xml)
		{
			if (xml == null || xml.Trim().Length == 0)
			{
				return new T();
			}
			using (StringReader sr = new StringReader(xml))
			{
				XmlSerializer xz = new XmlSerializer(typeof(T));
				return (T)xz.Deserialize(sr);
			}
		}
	}


	public static class XMLInfoOP
	{
		static private object lockobj = new object();

		/// <summary>
		/// 获取XML配置信息(可提供路径)
		/// </summary>
		/// <param name="url"></param>
		/// <returns></returns>
		public static object GetInfo(string url, Type T)
		{
			object data;
			XmlSerializer serializer = new XmlSerializer(T);
			try
			{
				string fileName = url;
				FileStream fs = new FileStream(fileName, FileMode.Open);
				data = serializer.Deserialize(fs);
				fs.Close();
			}
			catch
			{
				data = null;
			}
			return data;
		}

		/// <summary>
		/// 保存XML配置 (可提供路径)
		/// </summary>
		/// <param name="data"></param>
		/// <param name="url"></param>
		public static void SaveInfo(object data, string url, Type T)
		{
			FileOP.CreatFolder(url);
			string fileName = url;
			XmlSerializer serializer = new XmlSerializer(T);

			lock (lockobj)
			{
				FileStream fs = new FileStream(fileName, FileMode.Create);
				serializer.Serialize(fs, data);
				fs.Close();
			}
		}

		/// <summary>
		/// 将对象序列化成XML字符串
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		public static string GetXML(object data)
		{
			if (data == null) return "";
			using (StringWriter sw = new StringWriter())
			{
				XmlSerializer serializer = new XmlSerializer(data.GetType());
				serializer.Serialize(sw, data);
				return sw.ToString();
			}

		}
		/// <summary>
		/// 将XML字符串转换为指定对象
		/// </summary>
		/// <param name="xml"></param>
		/// <returns></returns>
		public static object GetObj(string xml, Type T)
		{
			if (xml == null || xml.Trim().Length == 0)
			{
				return Activator.CreateInstance(T);
			}
			using (StringReader sr = new StringReader(xml))
			{
				XmlSerializer xz = new XmlSerializer(T);
				return xz.Deserialize(sr);
			}
		}
	}

	#endregion
}
